﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;

namespace GhostGame
{
    public partial class GhostRoom : DataObjects.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.user = this.GetCurrentUser();
            if (!this.IsPostBack)
            {
                this.FreshBind();
            }
            else
            {
                this.room = DataObjects.GameContainer.Rooms.FirstOrDefault(r => r.ID == (int)this.ViewState["roomid"]);
                if (this.room == default(DataObjects.GhostRoom))
                {
                    this.Response.Redirect("~/Default.aspx");
                }
            }
        }

        private void FreshBind()
        {
            this.room = DataObjects.GameContainer.Rooms.FirstOrDefault(
                r => r.Holder.ID == this.user.ID);
            if (this.room == default(DataObjects.GhostRoom))
            {
                this.room = DataObjects.GameContainer.Rooms.FirstOrDefault(
                    r => r.Members.Select(m => m.ID).Contains(this.user.ID));
            }
            if (this.room != default(DataObjects.GhostRoom))
            {
                this.BindRoom(this.room);
                this.TBRoomName.Text = this.room.Name;
            }
            else
            {
                this.Response.Redirect("~/Default.aspx");
            }
        }

        private void BindRoom(DataObjects.GhostRoom room)
        {
            this.RoomMembers.DataSource = room.Members;
            this.RoomMembers.DataBind();
            this.GhostWord.Text = room.GhostWord;
            this.HumanWord.Text = room.HumanWord;
            this.room = room;
            this.ViewState["roomid"] = this.room.ID;
        }

        public DataObjects.GhostRoom room;
        public DataObjects.User user;

        protected void RoomMembers_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int userID = int.Parse(e.CommandArgument as string);
            switch (e.CommandName)
            {
                case "KickOut":
                    this.room.RemoveMember(userID);
                    this.BindRoom(this.room);
                    break;
                case "SetJudge":
                    this.room.SwitchJudge(userID);
                    this.BindRoom(this.room);
                    break;
                default:
                    break;
            }
        }

        protected void DiedCheckChanged(object sender, EventArgs e)
        {
            if (this.user.ID == this.room.Holder.ID)
            {
                DataObjects.User user = this.room.Members.First(
                    u => u.ID == this.GetControlBindUserID(sender as Control));
                user.Died = ((CheckBox)sender).Checked;
                this.room.UpdateTime();
            }
        }

        protected void GhostCheckedChanged(object sender, EventArgs e)
        {
            if (this.user.ID == this.room.Holder.ID)
            {
                DataObjects.User user = this.room.Members.First(
                    u => u.ID == this.GetControlBindUserID(sender as Control));
                user.IsGhost = ((CheckBox)sender).Checked;
                this.room.UpdateTime();
            }
        }

        protected void StartGame_Click(object sender, EventArgs e)
        {
            this.room.Status = DataObjects.GhostRoom.RoomStatus.GhostHunting;
            this.room.HumanWord = this.HumanWord.Text;
            this.room.GhostWord = this.GhostWord.Text;
            this.GhostWord.ReadOnly = true;
            this.HumanWord.ReadOnly = true;
            this.room.UpdateTime();
            this.BindRoom(this.room);
        }

        protected void RoomMembers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ((Label)e.Item.Controls[0]).Text = ((DataObjects.User)e.Item.DataItem).Name;
            ((CheckBox)e.Item.Controls[1]).Enabled = (this.user.ID == this.room.Holder.ID);
            ((CheckBox)e.Item.Controls[1]).Checked = ((DataObjects.User)e.Item.DataItem).Died;

            ((CheckBox)e.Item.Controls[2]).Checked = ((DataObjects.User)e.Item.DataItem).IsGhost;
            ((CheckBox)e.Item.Controls[2]).Enabled = this.room.Status == DataObjects.GhostRoom.RoomStatus.Waiting;

            ((Button)e.Item.Controls[3]).CommandArgument = ((DataObjects.User)e.Item.DataItem).ID.ToString();
            ((Button)e.Item.Controls[4]).CommandArgument = ((DataObjects.User)e.Item.DataItem).ID.ToString();
            ((Button)e.Item.Controls[4]).Enabled = this.room.Status == DataObjects.GhostRoom.RoomStatus.Waiting;
        }

        private int GetControlBindUserID(Control Item)
        {
            return int.Parse(((Button)Item.Parent.Controls[3]).CommandArgument);
        }

        protected void QuitGame_Click(object sender, EventArgs e)
        {
            if (this.room.Holder.ID == this.user.ID)
            {
                this.room.ClearRoom();
                DataObjects.GameContainer.Rooms.Remove(this.room);
            }
            else
            {
                this.room.RemoveMember(this.user.ID);
                this.room.UpdateTime();
            }
            this.Response.Redirect("~/Default.aspx");
        }

        protected void EndGame_Click(object sender, EventArgs e)
        {
            this.room.Status = DataObjects.GhostRoom.RoomStatus.Waiting;
            this.GhostWord.ReadOnly = false;
            this.HumanWord.ReadOnly = false;
            this.room.UpdateTime();
            foreach (var item in this.room.Members)
            {
                item.ClearStatus();
            }
            this.BindRoom(this.room);
        }

        protected void SetGame_Click(object sender, EventArgs e)
        {
            this.room.Name = this.TBRoomName.Text;
            this.room.UpdateTime();
        }

        protected void Refresh_Click(object sender, EventArgs e)
        {
            this.FreshBind();
        }
    }
}